package com.yf.course.modules.course.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yf.base.api.api.dto.PagingReqDTO;
import com.yf.base.utils.BeanMapper;
import com.yf.course.modules.course.dto.CourseFaceRecordDTO;
import com.yf.course.modules.course.dto.request.CourseFaceSaveReqDTO;
import com.yf.course.modules.course.entity.CourseFaceRecord;
import com.yf.course.modules.course.mapper.CourseFaceRecordMapper;
import com.yf.course.modules.course.service.CourseFaceRecordService;
import com.yf.system.modules.user.UserUtils;
import com.yf.system.modules.user.dto.response.FaceCheckRespDTO;
import com.yf.system.modules.user.service.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;

/**
* <p>
* 学习刷脸记录业务实现类
* </p>
*
* @author 聪明笨狗
* @since 2022-04-25 11:26
*/
@Service
public class CourseFaceRecordServiceImpl extends ServiceImpl<CourseFaceRecordMapper, CourseFaceRecord> implements CourseFaceRecordService {

    @Autowired
    private SysUserService sysUserService;

    @Override
    public IPage<CourseFaceRecordDTO> paging(PagingReqDTO<CourseFaceRecordDTO> reqDTO) {

        //查询条件
        QueryWrapper<CourseFaceRecord> wrapper = new QueryWrapper<>();

        // 请求参数
        CourseFaceRecordDTO params = reqDTO.getParams();
        if(params!=null){
            if(StringUtils.isNoneBlank(params.getCourseId())){
                wrapper.lambda().eq(CourseFaceRecord::getCourseId, params.getCourseId());
            }
            if(StringUtils.isNoneBlank(params.getUserId())){
                wrapper.lambda().eq(CourseFaceRecord::getUserId, params.getUserId());
            }
            if(StringUtils.isNoneBlank(params.getFileId())){
                wrapper.lambda().eq(CourseFaceRecord::getFileId, params.getFileId());
            }
        }

        wrapper.lambda().orderByDesc(CourseFaceRecord::getCreateTime);

        //获得数据
        IPage<CourseFaceRecord> page = this.page(reqDTO.toPage(), wrapper);
        //转换结果
        IPage<CourseFaceRecordDTO> pageData = JSON.parseObject(JSON.toJSONString(page), new TypeReference<Page<CourseFaceRecordDTO>>(){});
        return pageData;
    }


    @Override
    public FaceCheckRespDTO checkFace(CourseFaceSaveReqDTO reqDTO){

        // 进行人脸识别
        FaceCheckRespDTO respDTO = sysUserService.faceCheck(reqDTO.getBase64(), false);

        String userId = UserUtils.getUserId();

        // 存档
        CourseFaceRecord capture = new CourseFaceRecord();
        BeanMapper.copy(reqDTO, capture);
        capture.setUserId(userId);
        capture.setSuccess(respDTO.isPass());

        if(respDTO.isPass()){
            // 1天有效
            Calendar cl = Calendar.getInstance();
            cl.setTimeInMillis(System.currentTimeMillis());
            cl.add(Calendar.DATE, 1);
            capture.setExpireTime(cl.getTime());
        }

        capture.setImage(respDTO.getFaceImage());
        // 当前用户
        capture.setUserId(UserUtils.getUserId());
        capture.setCreateTime(new Date());

        this.save(capture);

        return respDTO;
    }

    @Override
    public boolean check(CourseFaceRecordDTO reqDTO) {

        // 当前用户
        String userId = UserUtils.getUserId();

        QueryWrapper<CourseFaceRecord> wrapper = new QueryWrapper();
        wrapper.lambda()
                .eq(CourseFaceRecord::getCourseId, reqDTO.getCourseId())
                .eq(CourseFaceRecord::getFileId, reqDTO.getFileId())
                .eq(CourseFaceRecord::getUserId, userId)
                .eq(CourseFaceRecord::getSuccess, true)
                .ge(CourseFaceRecord::getExpireTime, new Date());

        return this.count(wrapper) > 0;
    }

}
